package com.learn.eduadboot.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.learn.eduadboot.service.PromotionAdService;
import com.learn.eduapi.entity.ResponseDTO;
import com.learn.eduapi.entity.ad.PromotionAd;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 广告详情后台管理服务8001
 */
@RestController
@CrossOrigin
@RequestMapping("ad")
public class AdminAdController {
    @Autowired
    private PromotionAdService promotionAdService;
    /**
     * 获取广告详情列表
     * @param find
     * @param page
     * @param size
     * @return
     */
    @GetMapping("getAllPromotionAd")
    public ResponseDTO<List<PromotionAd>> getAllPromotionAd(@RequestParam("find")String find,
                                                               @RequestParam("page")Integer page,
                                                               @RequestParam("size")Integer size){
        ResponseDTO<List<PromotionAd>> responseDTO = new ResponseDTO<>();
        responseDTO.setContent(promotionAdService.getAllPromotionAd(find,(page-1)*size,size));
        responseDTO.setState(promotionAdService.getAllPromotionAd(find,0,0).size());
        responseDTO.setMessage("获取广告详情列表成功");
        return responseDTO;
    }

    /**
     * 修改广告详情
     * @param id
     * @param name
     * @param space_id
     * @param keyword
     * @param html_content
     * @param text
     * @param link
     * @param img
     * @param priority
     * @return
     */
    @GetMapping("updateAd")
    public ResponseDTO<PromotionAd> updateAd(@RequestParam("id")Integer id,
                                             @RequestParam("name")String name,
                                             @RequestParam("space_id")String space_id,
                                             @RequestParam("keyword")String keyword,
                                             @RequestParam("html_content")String html_content,
                                             @RequestParam("text")String text,
                                             @RequestParam("link")String link,
                                             @RequestParam("img")String img,
                                             @RequestParam("priority")String priority,
                                             @RequestParam("start_time") String startTime,
                                             @RequestParam("end_time")String endtime){
        ResponseDTO<PromotionAd> responseDTO = new ResponseDTO<>();
        PromotionAd promotionAd1 = promotionAdService.selectByName(name);

        if(promotionAd1!=null && promotionAd1.getId().equals(id)!=true){
            responseDTO.setState(500);
            responseDTO.setMessage("广告名重复");
        }else {
            QueryWrapper<PromotionAd> wrapper = new QueryWrapper<>();
            wrapper.eq("id",id);
            PromotionAd promotionAd = new PromotionAd();
            promotionAd.setName(name);
            promotionAd.setSpace_id(Integer.parseInt(space_id));
            promotionAd.setKeyword(keyword);
            promotionAd.setHtml_content(html_content);
            promotionAd.setText(text);
            promotionAd.setLink(link);
            promotionAd.setImg(img);
            promotionAd.setPriority(Integer.parseInt(priority));
            promotionAd.setStart_time(startTime);
            promotionAd.setEnd_time(endtime);
            int i = promotionAdService.updateAd(promotionAd, wrapper);
            if (i==1){
                responseDTO.setState(200);
            }else {
                responseDTO.setState(500);
                responseDTO.setMessage("广告详情修改失败");
            }
        }
        return responseDTO;
    }

    /**
     * 添加广告详情
     * @param name
     * @param space_id
     * @param keyword
     * @param html_content
     * @param text
     * @param link
     * @param img
     * @param priority
     * @return
     */
    @GetMapping("addAd")
    public ResponseDTO<PromotionAd> addAd(@RequestParam("name")String name,
                                          @RequestParam("space_id")String space_id,
                                          @RequestParam("keyword")String keyword,
                                          @RequestParam("html_content")String html_content,
                                          @RequestParam("text")String text,
                                          @RequestParam("link")String link,
                                          @RequestParam("img")String img,
                                          @RequestParam("priority")String priority,
                                          @RequestParam("start_time")String start_time,
                                          @RequestParam("end_time")String end_time){
        ResponseDTO<PromotionAd> responseDTO = new ResponseDTO<>();
        PromotionAd promotionAd1 = promotionAdService.selectByName(name);
        if(promotionAd1!=null){
            responseDTO.setState(500);
            responseDTO.setMessage("广告名重复");
        }else {
            PromotionAd promotionAd = new PromotionAd();
            promotionAd.setName(name);
            promotionAd.setSpace_id(Integer.parseInt(space_id));
            promotionAd.setKeyword(keyword);
            promotionAd.setHtml_content(html_content);
            promotionAd.setText(text);
            promotionAd.setLink(link);
            promotionAd.setImg(img);
            promotionAd.setPriority(Integer.parseInt(priority));
            promotionAd.setStart_time(start_time);
            promotionAd.setEnd_time(end_time);
            int i = promotionAdService.addAd(promotionAd);
            if (i==1){
                responseDTO.setState(200);
            }else {
                responseDTO.setState(500);
                responseDTO.setMessage("广告详情添加失败");
            }
        }
        return responseDTO;
    }

    /**
     * 取消/收藏广告
     * @param id
     * @param satuts
     * @return
     */
    @GetMapping("loveAd/{id}/{satuts}")
    public ResponseDTO<PromotionAd> loveAd(@PathVariable("id")Integer id,
                                           @PathVariable("satuts")String satuts){
        System.out.println("id = " + id);
        System.out.println("satuts = " + satuts);
        ResponseDTO<PromotionAd> responseDTO = new ResponseDTO<>();
        QueryWrapper<PromotionAd> wrapper = new QueryWrapper<>();
        wrapper.eq("id",id);
        PromotionAd promotionAd = new PromotionAd();
        promotionAd.setStatus(Integer.parseInt(satuts));
        int i = promotionAdService.updateAd(promotionAd, wrapper);
        if (i==1){
            responseDTO.setState(200);
        }else {
            responseDTO.setState(500);
            responseDTO.setMessage("取消/收藏广告");
        }
        return responseDTO;
    }

    /**
     * 删除广告
     * @param id
     * @return
     */
    @GetMapping("removeAd/{id}")
    public ResponseDTO<PromotionAd> removeAd(@PathVariable("id")Integer id){
        ResponseDTO<PromotionAd> responseDTO = new ResponseDTO<>();
        int i = promotionAdService.deleteAd(id);
        if (i==1){
            responseDTO.setState(200);
            responseDTO.setMessage("删除广告成功");
        }else {
            responseDTO.setState(500);
            responseDTO.setMessage("删除广告失败");
        }
        return responseDTO;
    }

}
